<script id="main_t_server" type="text/html">

<ul>

    <% for (var i = 0,len = list.length; i < len; i ++) { var item = list[i]; %>

		<li class="<%= item.className %>"><a href="<%= item.href %>"><label><%= item.name %></label><span><%= item.coun %></span></a></li>

    <% } %>

</ul>

</script>


<script id="main_t_event" type="text/html">

<ul>

    <% for (var i = 0,len = list.length; i < len; i ++) { var item = list[i]; %>

		<li class="<%= item.className %>"><a href="<%= item.href %>"><label><%= item.name %></label><span><%= item.coun %></span></a></li>

    <% } %>

</ul>

</script>

<script id="main_t_unhandled" type="text/html">

<ul>

    <% for (var i = 0,len = list.length; i < len; i ++) { var item = list[i]; %>

		<li class="<%= item[7] %>">
		<span>主机：</span><a href="<%= item[9] %>"><%= item[0] %></a><label class="<%= item[8] %>"></label>
		<p><span>事件项：</span><%= item[1] %></p>
		<button>查看状态信息</button>
		<div><%= item[6] %></div>
		</li>

    <% } %>

</ul>

</script>


<h3><a href="#monitor-monitorEvent">监控事件</a></h3>
<div id="event_listen">
</div>
<h3><a href="#server-serverList">主机</a></h3>
<div id="server_listen">
</div>
<h3><span>待处理事件列表</span></h3>
<div id="unhandled">
</div>
					

					
<script>
$(function(){
	
	var app = window.app;
	var conf = app.conf;
    var version = conf.version;
    var domain = conf.domain;
	var _url = conf._url;	
	
    var conf_1 = {
        url: _url + "/get/server/@all",
		isTips: 1
    };
    
    var conf_2 = {
        url: _url + "/get/status/@eventsummary"
    };
    
    var conf_3 = {
        url: _url + "/get/event/@unhandled"
    };
	
	//////////////////////////////////////////////////////////
	
	var serverCall = function(e, json){
    
        var $th = $(this), num = 0, count = 0, notMonitorNum = 0, data, html;
        
        //$th.html(JSON.stringify(json));
        ///////////////////////////////////////////////
        
        if (json == null) {
            $th.html('<div class="error">服务器返回的数据为空</div>');
            return;
        }
        for (var k in json) {
            $.each(json[k], function(k, v){
                if (k == 0) {
                    switch (v) {
                        case "0":
                            num++;
                            break;
                        case "1":
                            count++;
                            break;
                        case "2":
                            count++;
                            break;
                        case "3":
                            count++;
                            break;
                        case "4":
                            count++;
                            break;
                        case "5":
                            notMonitorNum++;
                            break;
                    }
                }
            });
        }
        
        /////////////////////////////////////////////// 
        
        data = {
            list: [{
                className: '',
                href: '#server-serverList?status=down',
                name: '宕机',
                coun: num
            }, {
                className: '',
                href: '#server-serverList?status=up',
                name: '在线',
                coun: count
            }, {
                className: '',
                href: '#server-serverList?status=unmonitored',
                name: '未监控',
                coun: notMonitorNum
            }]
        };
        
        ///////////////////////////////////////////
        html = template('main_t_server', data);
        $th.html(html);
        
    };	
	var eventCall = function(e, json){
        var $th = $(this), data, arr = [], i, o, html, conf = {
            'warning': '严重事件',
            'caution': '注意事件',
            'ok': '正常'
        };
        
        //$th.html(JSON.stringify(json));return;
        ///////////////////////////////////////////
        
        if (json == null) {
            $th.html('<div class="error">服务器返回的数据为空</div>');
            return;
        }
        
        for (var i in json) {
            o = {};
            o.name = conf[i];
            o.coun = json[i];
			o.className = '';
            o.href = '#monitor-monitorEvent?eventStatus=' + i;
            arr.push(o);
        }
        
        ///////////////////////////////////////////
        data = {
            list: arr
        };
        html = template('main_t_event', data);
        $th.html(html);
        
    };
	var unhandledCall = function(e, json){
        var $th = $(this), data, html;
        
        //$th.html(JSON.stringify(json));return;
        ///////////////////////////////////////////
        
        if (json == null) {
            $th.html('<div class="error">没有需要处理的事件</div>');
            return;
        }
        
        $.each(json, function(k, v){
            switch (v[3]) {
                case 2:
                    v.push('state_4');
                    v.push('icon_caution');
                    break;
                case 3:
                    v.push('state_4');
                    v.push('icon_warning');
                    break;
                default:
            }
            v.push('#server-serverStatus?name=' + v[0]);
        });
        
        data = {
            list: json
        };
        html = template('main_t_unhandled', data);
        $th.html(html);
        
    };
	
	
	var layerId = 'main';
	
    var $parent = $('#' + layerId);
    var $server = $parent.find('#server_listen');
    var $event = $parent.find('#event_listen');
    var $unhandled = $parent.find('#unhandled');
    
    
    /////////////////////////////////////////////////////////
    
    $server.bind('ajax.success', serverCall);
    
    $event.bind('ajax.success', eventCall);
    
    $unhandled.bind('ajax.success', unhandledCall);
    
    $unhandled.delegate('button', 'click', function(){
        var $th = $(this);
        if ($th.text() == '查看状态信息') {
            $th.next().show();
            $th.text('收起状态信息')
        }
        else {
            $th.next().hide();
            $th.text('查看状态信息')
        }
        
    });	
	
	/////////////////////////////////////////////////////
    
    var controler = {
        //自定义下拖页面事件处理函数
        pullDownAction: function(){
            this.recover();return;
            conf_1.isLoading = 1;
            $server.getData(conf_1);
            conf_2.isLoading = 1;
            $event.getData(conf_2);
            conf_3.isLoading = 1;
            $unhandled.getData(conf_3);
        },
        init: function(param){
            this.recover = function(){
                //请求服务器监听数据
                $server.getData(conf_1);
                //请求事件监听数据
                $event.getData(conf_2);
                //未处理事件监听数据
                $unhandled.getData(conf_3);
            };
            
            this.recover();
        }
        
        
    };
	
	//////////////////////////////////////////////////
	var layer = extend(app.face[layerId],controler);
	layer.init();

	
});
</script>
